#include "picturecaptionwidget.h"
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QPainterPath>
#include <QToolTip>

PictureCaptionWidget::PictureCaptionWidget(ToolType toolType,
                                           const QPixmap &image,
                                           const QString &title,
                                           const QString &description,
                                           QWidget *parent)
    : QWidget(parent)
    , m_toolType(toolType)
    , m_isHovered(false)
{
    // 创建图片标签
    m_imageLabel = new QLabel(this);
    m_imageLabel->setPixmap(image.scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation));

    // 创建标题标签
    m_titleLabel = new QLabel(title, this);
    m_titleLabel->setAlignment(Qt::AlignCenter);
    m_titleLabel->setWordWrap(false); // 不换行
    m_titleLabel->setToolTip(title);  // 设置工具提示为完整标题
    m_titleLabel->setStyleSheet("color: #333333;");
    QFont titleFont("Segoe UI", 12, QFont::Bold);
    m_titleLabel->setFont(titleFont);

    // 创建描述标签
    m_descriptionLabel = new QLabel(description, this);
    m_descriptionLabel->setAlignment(Qt::AlignCenter);
    m_descriptionLabel->setWordWrap(false);      // 不换行
    m_descriptionLabel->setToolTip(description); // 设置工具提示为完整描述
    m_descriptionLabel->setStyleSheet("color: #666666;");
    QFont descFont("Segoe UI", 10);
    m_descriptionLabel->setFont(descFont);

    // 创建布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_imageLabel, 0, Qt::AlignCenter);
    mainLayout->addSpacing(10);
    mainLayout->addWidget(m_titleLabel);
    mainLayout->addSpacing(5);
    mainLayout->addWidget(m_descriptionLabel);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 设置控件大小策略
    setFixedSize(200, 250); // 固定控件大小

    // 添加阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(10);
    shadowEffect->setColor(QColor(0, 0, 0, 80));
    shadowEffect->setOffset(0, 3);
    setGraphicsEffect(shadowEffect);
}

void PictureCaptionWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 根据是否悬停设置背景颜色
    QColor bgColor = m_isHovered ? QColor(230, 230, 230) : QColor(240, 240, 240);

    // 绘制圆角矩形背景
    QRect rect = this->rect();
    QPainterPath path;
    path.addRoundedRect(rect, 10, 10);
    painter.fillPath(path, bgColor);

    // 绘制边框
    painter.setPen(QPen(QColor(200, 200, 200), 2));
    painter.drawPath(path);
}

void PictureCaptionWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        emit clicked(m_toolType);
    }
    QWidget::mousePressEvent(event);
}

void PictureCaptionWidget::enterEvent(QEvent *event)
{
    m_isHovered = true;
    update();
    QWidget::enterEvent(event);
}

void PictureCaptionWidget::leaveEvent(QEvent *event)
{
    m_isHovered = false;
    update();
    QWidget::leaveEvent(event);
}
